package com.awspaas.user.apps.shhtaerospaceindustrial.controller;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.commons.formfile.model.delegate.FormFile;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

import java.util.List;
import java.util.Map;

@Controller
public class GetShiTangInfoController {
	
	/**
	 * @Desc 根据日期查询食堂提供的自选菜信息
	 * @param uc
	 * @param
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.shhtaerospaceindustrial_getWeekZiXuanCaiInfo")
	public static String getWeekZiXuanCaiInfo(UserContext uc, String choicedate) {
		JSONObject result = new JSONObject();
		try {
			String queryUrl =
					"SELECT BINDID,ID,CAIPINTYPE,CAIPINNAME,CAIPINRESUME,SUPPLYTIME "+
							" FROM " +
							" BO_EU_SHHT_WEEKZXC_BODYA " +
							" WHERE " +
							" to_char(appdate,'YYYY-MM-DD') =" +
							" '"+choicedate+"' ";
			List<Map<String, Object>> dataList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(dataList == null || dataList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到该日期食堂提供的自选菜品信息.");
				return result.toString();
			}
			JSONArray  zixuanArr = new JSONArray();
			for (int i = 0; i < dataList.size(); i++) {
				Map<String, Object> dataMap = dataList.get(i);
				JSONObject tececaitem = new JSONObject();
				String caipintype = CoreUtil.objToStr(dataMap.get("CAIPINTYPE"));//菜品类型
				String caipinname = CoreUtil.objToStr(dataMap.get("CAIPINNAME"));//菜品名称
				String supplytime = CoreUtil.objToStr(dataMap.get("SUPPLYTIME"));//时段
				String caipinresume = CoreUtil.objToStr(dataMap.get("CAIPINRESUME"));
				String bindid = CoreUtil.objToStr(dataMap.get("BINDID"));
				String  boId= CoreUtil.objToStr(dataMap.get("ID"));


				tececaitem.put("caipinname", caipinname);
				tececaitem.put("caipinresume", caipinresume);
				tececaitem.put("caipintype", caipintype);
				tececaitem.put("supplytime",supplytime);
				tececaitem.put("bindid", bindid);
				zixuanArr.add(tececaitem);
			}

//				SSOUtil ssoUtil = new SSOUtil();
				//根据文件创建者创建sid，formFile.getCreateUser()为userId
//				String sidPc = ssoUtil.registerClientSessionNoPassword(target, "cn", "", "pc");
//				String sidMobile = ssoUtil.registerClientSessionNoPassword(target, "cn", "", "mobile");
//				String portalUrl = SDK.getPortalAPI().getPortalUrl();//http://localhost:8088/portal
//				String urlPc = portalUrl + "/r/w?sid="+sidPc+"&cmd=CLIENT_BPM_FORM_MAIN_PAGE_OPEN&processInstId="+processInstId+"&openState=2&taskInstId="+taskInstId+"&displayToolbar=true";
//				String urlMobile = portalUrl + "/r/w?sid="+sidMobile+"&cmd=CLIENT_BPM_FORM_MAIN_PAGE_OPEN&processInstId="+processInstId+"&openState=2&taskInstId="+taskInstId+"&displayToolbar=true";
				result.put("status", "0");
				result.put("tececaiArr", zixuanArr);


		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}


	/**
	 * @Desc 根据食堂编号查询该食堂的特色菜信息
	 * @param uc
	 * @param
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.shhtaerospaceindustrial_getTeSeCaiInfo")
	public static String getTeSeCaiInfo(UserContext uc, String shitangcode) {
		JSONObject result = new JSONObject();
		try {
			String queryUrl =
					"SELECT BINDID,ID,LOCATION,AREA,CAIPINTYPE,CAIPINNAME,TESEWINDOW,CAIPINPRICE,CAIPINRESUME," +
							"YINGYANGVALUE," +
							"NOTE " +
							"FROM " +
							"BO_EU_SHSYST_CAIDANHEAD " +
							"WHERE " +
							"AREA =" +
							" '"+shitangcode+"' and CAIPINTYPE='特色菜'";
			List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(urlList == null || urlList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到该食堂提供的特色菜品信息.");
				return result.toString();
			}
			JSONArray tececaiArr = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject tececaitem = new JSONObject();
				String location = CoreUtil.objToStr(urlMap.get("LOCATION"));
				String area = CoreUtil.objToStr(urlMap.get("AREA"));
				String caipintype = CoreUtil.objToStr(urlMap.get("CAIPINTYPE"));
				String caipinname = CoreUtil.objToStr(urlMap.get("CAIPINNAME"));
				String tecewindow = CoreUtil.objToStr(urlMap.get("TESEWINDOW"));
				Double caipinprice = CoreUtil.objToDouble(urlMap.get("CAIPINPRICE"));
				String caipinresume = CoreUtil.objToStr(urlMap.get("CAIPINRESUME"));
				String yingyangvale = CoreUtil.objToStr(urlMap.get("YINGYANGVALUE"));
				String note = CoreUtil.objToStr(urlMap.get("NOTE"));
				String bindid = CoreUtil.objToStr(urlMap.get("BINDID"));
				String boId = CoreUtil.objToStr(urlMap.get("ID"));

				tececaitem.put("location", location);
				tececaitem.put("area", area);
				tececaitem.put("caipinname", caipinname);
				tececaitem.put("tecewindow", tecewindow);
				tececaitem.put("caipinprice", caipinprice);
				tececaitem.put("caipinresume", caipinresume);
				tececaitem.put("yingyangvale", yingyangvale);
				tececaitem.put("caipintype", caipintype);
				tececaitem.put("boId", boId);

				List<FormFile> files = SDK.getBOAPI().getFiles(boId, "CAIPINPHOTO");
				for(FormFile file : files){
					String downloadUrl = SDK.getBOAPI().getFileDCContext(file).getDownloadURL();
					tececaitem.put("downloadUrl",downloadUrl);
				}

				tececaiArr.add(tececaitem);
			}

//				SSOUtil ssoUtil = new SSOUtil();
				//根据文件创建者创建sid，formFile.getCreateUser()为userId
//				String sidPc = ssoUtil.registerClientSessionNoPassword(target, "cn", "", "pc");
//				String sidMobile = ssoUtil.registerClientSessionNoPassword(target, "cn", "", "mobile");
//				String portalUrl = SDK.getPortalAPI().getPortalUrl();//http://localhost:8088/portal
//				String urlPc = portalUrl + "/r/w?sid="+sidPc+"&cmd=CLIENT_BPM_FORM_MAIN_PAGE_OPEN&processInstId="+processInstId+"&openState=2&taskInstId="+taskInstId+"&displayToolbar=true";
//				String urlMobile = portalUrl + "/r/w?sid="+sidMobile+"&cmd=CLIENT_BPM_FORM_MAIN_PAGE_OPEN&processInstId="+processInstId+"&openState=2&taskInstId="+taskInstId+"&displayToolbar=true";
				result.put("status", "0");
				result.put("tececaiArr", tececaiArr);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
	
	
	/**
	 * @Desc 根据食堂编号查询该食堂的特色菜档口
	 * @param uc
	 * @param
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.shhtaerospaceindustrial_getTeSeCaiDangKouInfo")
	public static String getTeSeCaiDangKouInfo(UserContext uc, String shitangcode) {
		JSONObject result = new JSONObject();
		try {
			String queryUrl ="SELECT distinct(TESEWINDOW),AREA FROM BO_EU_SHSYST_CAIDANHEAD " +
							" WHERE AREA = '"+shitangcode+"' and CAIPINTYPE='特色菜'";
			List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(urlList == null || urlList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到该食堂提供的特色菜品信息.");
				return result.toString();
			}
			JSONArray tececaiArr = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject tececaitem = new JSONObject();
				String area = CoreUtil.objToStr(urlMap.get("AREA"));//食堂id
				String tecewindow = CoreUtil.objToStr(urlMap.get("TESEWINDOW"));//食堂档口
				tececaitem.put("area", area);
				tececaitem.put("tecewindow", tecewindow);
				tececaiArr.add(tececaitem);
			}
				result.put("status", "0");
				result.put("tececaiArr", tececaiArr);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
	
	/**
	 * @Desc 查询所有食堂和食堂的特色菜档口
	 * @param uc
	 * @param
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.shhtaerospaceindustrial_getTeSeCaiSuoYouShiTangDangKouInfo")
	public static String getTeSeCaiSuoYouShiTangDangKouInfo(UserContext uc) {
		JSONObject result = new JSONObject();//总数据输出
		JSONArray checkresult = new JSONArray(); //根据食堂区域查询档口数据
		try {
			//查询食堂区域id
			String quyuUrl ="select item.cnname, item.itemno from Bo_Act_Dict_Kv_Item item " + 
					" left join BO_ACT_DICT_KV_MAIN main on item.bindid = main.bindid where  "
					+ " main.dictkey = 'shdininghallarea' and item.orderindex < 200   order by item.orderindex  asc";
			List<Map<String, Object>> quyuList = DBSql.query(quyuUrl, new ColumnMapRowMapper(), new Object[]{});
			if(quyuList == null || quyuList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到食堂信息.");
				return result.toString();
			}
			for (int i = 0; i < quyuList.size(); i++) {
			    JSONObject zhongjianresult = new JSONObject(); //中间查询结果
				JSONArray tececaiArr = new JSONArray();
				Map<String, Object>  quyuMap = quyuList.get(i);
				String cnname = CoreUtil.objToStr(quyuMap.get("cnname"));//食堂名称
				String areaid = CoreUtil.objToStr(quyuMap.get("itemno"));//食堂id
				zhongjianresult.put("id", areaid);
				zhongjianresult.put("text", cnname);
				String queryUrl ="SELECT distinct(TESEWINDOW),AREA FROM BO_EU_SHSYST_CAIDANHEAD " +
						" WHERE AREA = '"+areaid+"' and CAIPINTYPE='特色菜'";
				List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
				if(urlList == null || urlList.isEmpty()) {
					JSONObject tececaitem = new JSONObject();
					tececaitem.put("text", "");
					tececaiArr.add(tececaitem);
					
				}else {
					for (int j = 0; j < urlList.size(); j++) {
						Map<String, Object> urlMap = urlList.get(j);
						JSONObject tececaitem = new JSONObject();
						String tecewindow = CoreUtil.objToStr(urlMap.get("TESEWINDOW"));//食堂档口
						tececaitem.put("text", tecewindow);
						tececaiArr.add(tececaitem);
					}
				}
				zhongjianresult.put("children", tececaiArr);
				checkresult.add(zhongjianresult);
			}
			
			result.put("status", "0");
			result.put("item", checkresult);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
	
	/**
	 * @Desc 根据食堂编号和档口，查询该食堂档口的特色菜信息
	 * @param uc
	 * @param shitangcode  食堂ID
	 * @param dangkou   食堂档口
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.shhtaerospaceindustrial_getDangKouTeSeCaiInfo")
	public static String getDangKouTeSeCaiInfo(UserContext uc, String shitangcode,String dangkou) {
		JSONObject result = new JSONObject();
		try {
			String queryUrl =
					"SELECT BINDID,ID,LOCATION,AREA,CAIPINTYPE,CAIPINNAME,TESEWINDOW,CAIPINPRICE,MEAUNIT,CAIPINRESUME," +
							" YINGYANGVALUE,NOTE FROM BO_EU_SHSYST_CAIDANHEAD " +
							" WHERE  AREA =  '"+shitangcode+"' and TESEWINDOW =  '"+dangkou+"' and CAIPINTYPE='特色菜'";
			List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(urlList == null || urlList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到该食堂提供的特色菜品信息.");
				return result.toString();
			}
			JSONArray tececaiArr = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject tececaitem = new JSONObject();
				String location = CoreUtil.objToStr(urlMap.get("LOCATION"));
				String area = CoreUtil.objToStr(urlMap.get("AREA"));

				String caipintype = CoreUtil.objToStr(urlMap.get("CAIPINTYPE"));
				String caipinname = CoreUtil.objToStr(urlMap.get("CAIPINNAME"));
				String tecewindow = CoreUtil.objToStr(urlMap.get("TESEWINDOW"));
				Double caipinprice = CoreUtil.objToDouble(urlMap.get("CAIPINPRICE"));
				String meaunit = CoreUtil.objToStr(urlMap.get("MEAUNIT"));
				String caipinresume = CoreUtil.objToStr(urlMap.get("CAIPINRESUME"));
				String yingyangvale = CoreUtil.objToStr(urlMap.get("YINGYANGVALUE"));
				String note = CoreUtil.objToStr(urlMap.get("NOTE"));
				String bindid = CoreUtil.objToStr(urlMap.get("BINDID"));
				String boId = CoreUtil.objToStr(urlMap.get("ID"));

				tececaitem.put("location", location);
				tececaitem.put("area", area);
				tececaitem.put("caipinname", caipinname);
				tececaitem.put("tecewindow", tecewindow);
				tececaitem.put("caipinprice", caipinprice);
				tececaitem.put("meaunit", meaunit);
				tececaitem.put("caipinresume", caipinresume);
				tececaitem.put("yingyangvale", yingyangvale);
				tececaitem.put("caipintype", caipintype);
				tececaitem.put("boId", boId);

				List<FormFile> files = SDK.getBOAPI().getFiles(boId, "CAIPINPHOTO");
				for(FormFile file : files){
					String downloadUrl = SDK.getBOAPI().getFileDCContext(file).getDownloadURL();
					tececaitem.put("downloadUrl",downloadUrl);
				}

				tececaiArr.add(tececaitem);
			}
				result.put("status", "0");
				result.put("tececaiArr", tececaiArr);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
	
	//外卖
	/**
	 * @Desc 根据菜品类型为“外卖”查询所有的特色菜档口
	 * @param uc
	 * @param
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.WaiMaiTypeCaiDangKouInfo")
	public static String getTeSeWaiMaiCaiDangKouInfo(UserContext uc) {
		JSONObject result = new JSONObject();
		try {
			String queryUrl ="SELECT distinct(TESEWINDOW),AREA FROM BO_EU_SHSYST_CAIDANHEAD " +
							" WHERE CAIPINTYPE='外卖'";
			List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(urlList == null || urlList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到提供的外卖菜品信息.");
				return result.toString();
			}
			JSONArray tececaiArr = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject tececaitem = new JSONObject();
				String area = CoreUtil.objToStr(urlMap.get("AREA"));//食堂id
				String tecewindow = CoreUtil.objToStr(urlMap.get("TESEWINDOW"));//食堂档口
				tececaitem.put("area", area);
				tececaitem.put("tecewindow", tecewindow);
				tececaiArr.add(tececaitem);
			}
				result.put("status", "0");
				result.put("tececaiArr", tececaiArr);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
	
	/**
	 * @Desc 根据特色档口，查询该档口的外卖商品信息
	 * @param uc
	 * @param shitangcode  食堂ID
	 * @param dangkou   食堂档口
	 * @return
	 */
	@Mapping("com.awspaas.user.apps.WaiMaiDangKouTeSeCaiInfo")
	public static String getWaiMaiDangKouTeSeCaiInfo(UserContext uc, String dangkou) {
		JSONObject result = new JSONObject();
		try {
			String queryUrl =
					"SELECT BINDID,ID,LOCATION,AREA,CAIPINTYPE,CAIPINNAME,TESEWINDOW,CAIPINPRICE,MEAUNIT,CAIPINRESUME," +
							" YINGYANGVALUE,NOTE FROM BO_EU_SHSYST_CAIDANHEAD " +
							" WHERE TESEWINDOW =  '"+dangkou+"' and CAIPINTYPE='外卖'";
			List<Map<String, Object>> urlList = DBSql.query(queryUrl, new ColumnMapRowMapper(), new Object[]{});
			if(urlList == null || urlList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到该档口提供的外卖商品信息.");
				return result.toString();
			}
			JSONArray tececaiArr = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject tececaitem = new JSONObject();
				String location = CoreUtil.objToStr(urlMap.get("LOCATION"));
				String area = CoreUtil.objToStr(urlMap.get("AREA"));

				String caipintype = CoreUtil.objToStr(urlMap.get("CAIPINTYPE"));
				String caipinname = CoreUtil.objToStr(urlMap.get("CAIPINNAME"));
				String tecewindow = CoreUtil.objToStr(urlMap.get("TESEWINDOW"));
				Double caipinprice = CoreUtil.objToDouble(urlMap.get("CAIPINPRICE"));
				String meaunit = CoreUtil.objToStr(urlMap.get("MEAUNIT"));
				String caipinresume = CoreUtil.objToStr(urlMap.get("CAIPINRESUME"));
				String yingyangvale = CoreUtil.objToStr(urlMap.get("YINGYANGVALUE"));
				String note = CoreUtil.objToStr(urlMap.get("NOTE"));
				String bindid = CoreUtil.objToStr(urlMap.get("BINDID"));
				String boId = CoreUtil.objToStr(urlMap.get("ID"));

				tececaitem.put("location", location);
				tececaitem.put("area", area);
				tececaitem.put("caipinname", caipinname);
				tececaitem.put("tecewindow", tecewindow);
				tececaitem.put("caipinprice", caipinprice);
				tececaitem.put("meaunit", meaunit);
				tececaitem.put("caipinresume", caipinresume);
				tececaitem.put("yingyangvale", yingyangvale);
				tececaitem.put("caipintype", caipintype);
				tececaitem.put("boId", boId);

				List<FormFile> files = SDK.getBOAPI().getFiles(boId, "CAIPINPHOTO");
				for(FormFile file : files){
					String downloadUrl = SDK.getBOAPI().getFileDCContext(file).getDownloadURL();
					tececaitem.put("downloadUrl",downloadUrl);
				}

				tececaiArr.add(tececaitem);
			}
				result.put("status", "0");
				result.put("tececaiArr", tececaiArr);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}
}
